package auctionhaus

import org.hibernate.validator.constraints.Email
import java.security.PublicKey
import org.h2.util.DateTimeUtils

class Listing {
    String name
    Date dateCreated
    Date lastUpdated
    Date endDateTime
    Double startingBidAmount
    String description
    Customer winner
    Boolean isClosed = false
    static belongsTo = [seller:Customer]
    static hasMany = [bids:Bid]


    static constraints = {
        //Listing name must not be null and must be less than 64 characters
        name(size:1..63, blank: false)
        //Optional description must be less than 256 characters
        description(nullable:true, size:0..255)
        //Winner field can be null
        winner(nullable:true)
        endDateTime(validator: {val, obj->
            if (obj.id == null) {
                if (!val.after(new Date())) {
                    return 'notinfuture'
                }
            }
            return true
        })
    }

    static namedQueries = {
        openListings  {
            def now = new Date()
            gt 'endDateTime', now
        }
        
        closedListings {
            def now = new Date()
            le 'endDateTime', now
        }
    }

}
